
module MyTreeWidgetItem
    def eachChild()
        for i in 0...childCount
            yield child(i)
        end
    end
end

# Hash -t szerkeszt; exportal, importal Ruby strukturaba
class HashEditorWidget < Qt::Dialog
    slots 'newClient()'
    slots 'newMember()'
    slots 'delete()'
    
    attr_reader :tv
    
    def initialize()
        super()
        
        toolbar = Qt::ToolBar.new
        a = toolbar.addAction "New client"
        connect a, SIGNAL('triggered()'), self, SLOT('newClient()')
        a = toolbar.addAction "New member"
        connect a, SIGNAL('triggered()'), self, SLOT('newMember()')
        a = toolbar.addAction KDE::Icon.new("mail-receive"), "Delete"
        connect a, SIGNAL('triggered()'), self, SLOT('delete()')
        
        @tv = Qt::TreeWidget.new
        @tv.setColumnCount 1
        
        layout = Qt::VBoxLayout.new self
        layout.addWidget toolbar
        layout.addWidget @tv
        setLayout layout
        
        show
    end
    
    def addCategory(name)
        @i = Qt::TreeWidgetItem.new @tv
        @i.setText 0, name
        @i.setFlags(@i.flags | Qt::ItemIsEditable.to_i)
        @i.extend MyTreeWidgetItem
        return @i
    end
    
    def addMember(name, parent = nil)
        i = nil
        if parent
            i = Qt::TreeWidgetItem.new parent
        else
            i = Qt::TreeWidgetItem.new @i
        end
        i.setText 0, name
        i.setFlags(i.flags | Qt::ItemIsEditable.to_i)
        return i
    end
    
    def newItem(type)
        ci = @tv.currentItem
        # add/setedit/rename
        #p "new cat/member! #{ci} #{ci.parent}"
        i = nil
        case type
        when "client"
            i = addCategory "New client..."
        when "member"
            parent = ci.parent
            parent = ci unless parent
            i = addMember "New member...", ci.parent
        else
            raise "WTF??"
        end
        @tv.setCurrentItem i
        @tv.editItem i
    end
    def newClient()
        newItem "client"
    end
    def newMember()
        newItem "member"
    end
    
    def delete()
        ci = @tv.currentItem
        p "del cat/member! #{ci} #{ci.parent}"
        if ci.parent
            ci.parent.removeChild ci
        else
            index = @tv.indexOfTopLevelItem ci
            @tv.takeTopLevelItem index
            #@tv.removeItemWidget ci, 0
        end
    end
    
    
    def getItemTree()
        h = {}
        each { |c|
            #p "#{c.text(0)}"
            a = []
            c.eachChild { |m|
                #p " #{m.text(0)}"
                a << m.text(0)
            }
            h[c.text(0)] = a
        }
        return h
    end
    
    def setItemTree(itemtree)
        itemtree.each { |c, v|
            addCategory c
            v.each { |m|
                addMember m
            }
        }
    end

    private
    def each()
        #p "topLevelItemCount: #{@tv.topLevelItemCount}"
        for i in 0...@tv.topLevelItemCount
            yield @tv.topLevelItem(i)
        end
    end
end

# menti/visszallitja az adott Hash -t
class PersistentHash
    attr_reader :hash
    
    def initialize(modul)
        @hash = {}
        $settings.beginGroup modul
        $settings.childGroups.each { |c|
            size = $settings.beginReadArray c
            puts "size: #{size}"
            for i in 0...size
                $settings.setArrayIndex i
                v = $settings.value("value").toString
                puts "value: #{v}"
                (@hash[c] ||= []) << v
            end
            $settings.endArray
        }
        #$settings.endGroup
    end
    
    def hash=(value)
        @hash = value
        
        $settings.remove("")
        
        value.each { |k, v|
            $settings.beginWriteArray k
            i = 0
            v.each { |m|
                $settings.setArrayIndex i
                $settings.set_value "value", Qt::Variant.new(m)
                i += 1
            }
            $settings.endArray
        }
        $settings.sync
    end
end
